//获取应用实例
const app = getApp();

Page({
	data: {
    /**
     * 图片
     */
    imgs: [],
    list: '',
    upload_picture_list: [],
  },
	onLoad: function(options) {
    this.orderdetail(options.order_detail_id);
  },

 
  orderdetail: function (order_id) {
    // console.log(order_id)
    var that = this;
    wx.request({
      url: app.globalData.api + '/index.php/wap/Order/getMsg',
      data: {
        token: app.globalData.token,
        order_id: order_id,
      },
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success(res) {
        console.log(res)
        if (res.data.data.orderMsg.detail.length > 0) {
          res.data.data.orderMsg.detail[0].img = app.globalData.imgUrl + "" + res.data.data.orderMsg.detail[0].img;
          that.setData({
            orderdetail: res.data.data.orderMsg
          })
        }
      }
    })
  },

  //填写退货快递单号
  submitBack:function(){
    var that = this;
    console.log(that.data.orderdetail.detail[0].order_detail_id)
    wx.request({
      url: app.globalData.api + '/index.php/wap/Order/fillInRefundExpressNum',
      data: {
        token: app.globalData.token,
        logistics: '测试物流',
        express_number:'0001111111',
        order_detail_id: that.data.orderdetail.detail[0].order_detail_id
      },
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success(res) {
        console.log(res)
        if(res.data.code==1){

        }
        wx.showToast({
          title: '' + res.data.msg,
          icon: 'none',
          duration: 1000
        })
      }
    })
  },


  //选择图片方法
  uploadpic: function (e) {
    let that = this //获取上下文
    let upload_picture_list = that.data.upload_picture_list
    //选择图片
    wx.chooseImage({
      count: 8, // 默认9，这里显示一次选择相册的图片数量 
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有  
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) { // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片 
        console.log(res)
        let tempFiles = res.tempFilePaths
        //把选择的图片 添加到集合里
        for (let i in tempFiles) {
          // tempFiles[i]['upload_percent'] = 0
          // tempFiles[i]['path_server'] = ''
          upload_picture_list.push(tempFiles[i])
        }
        //显示
        that.setData({
          upload_picture_list: upload_picture_list,
        });

        console.log(that.data.upload_picture_list);
      }
    })
  },
  //点击上传图片
  uploadimage() {
    let page = this
    let upload_picture_list = page.data.upload_picture_list
    console.log(upload_picture_list[0])
    console.log(upload_picture_list.length)
    for (let i = 0; i < upload_picture_list.length; i++) {
      wx.uploadFile({
        url: app.globalData.api + '/index.php/wap/Order/comment', //此处换上你的接口地址 
        filePath: upload_picture_list[i],
        name: 'img',
        header: {
          "Content-Type": "multipart/form-data",
        },
        formData: {
          'token': app.globalData.token,
          'order_detail_id': page.data.order_detail_id,
          'score': page.data.flag,
          'grade': 1,
          'content': page.data.content,
          'img': 'filePath',
        }, // HTTP 请求中其他额外的 form data
        success: function (res) {
          console.log(res);
          let data = JSON.parse(res.data); // 这个很关键
        }, fail: function (res) {
          console.log(res)
        }
      })
    }
    //循环把图片上传到服务器 并显示进度       
    // for (let j in upload_picture_list) {
    // if (upload_picture_list[j]['upload_percent'] == 0) {

    //上传图片后端地址
    // upload_file_server(app.globalData.api + '/index.php/wap/Order/comment', page, upload_picture_list, j)
    // }
    // }
    let imgs = wx.setStorageSync('imgs', upload_picture_list);
  },
  // 点击删除图片
  deleteImg(e) {
    let upload_picture_list = this.data.upload_picture_list;
    let index = e.currentTarget.dataset.index;
    upload_picture_list.splice(index, 1);
    this.setData({
      upload_picture_list: upload_picture_list
    });
    console.log(this.data.upload_picture_list)
  },
  // 预览图片
  previewImg(e) {
    //获取当前图片的下标
    let index = e.currentTarget.dataset.index;
    //所有图片
    let imgs = this.data.imgs;
    wx.previewImage({
      //当前显示图片
      current: imgs[index],
      //所有图片
      urls: imgs
    })
  },
});
